/**
 * Created by dukes on 2016/2/24.
 */
class ShieldCommentPanel extends eui.Component{
    private frameItem:FrameItem;
    private shieldBuff:BuffInfo;
    public constructor(){
        super();
        this.skinName = "pet.ShieldCommentSkin";
        this.name = "ShieldCommentSkin";
        this.frameItem = new FrameItem();
        this.frameItem.callFun = this.update;
        this.frameItem.callObj = this;
    }

    public update(delta:number){
        var gap = this.shieldBuff.endTime - Model.SYSTIME;
        if( gap > 0){
            var timestr = GetTimeStr(gap,2,true);;
            this.shield_timer.text = timestr;
        }else{
            FrameMgr.getInstance().delController(this.name);
        }
    }

    private img_close:eui.Image;
    private shield_timer:eui.BitmapLabel;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_close") {
            this.img_close = instance;
            this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        } else if( name == "shield_timer"){
            this.shield_timer = instance;
            this.shield_timer.textAlign = "center";
        }
    }

    private onClose(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
        FrameMgr.getInstance().delController(this.name);
    }

    public show(){
        if( !DisplayController.instance().stage) return;
        this.shieldBuff = BuffMgr.getInstance().getBuffById(BuffMgr.SHIELD_BUFF);
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
        if( this.shieldBuff && this.shieldBuff.endTime > Model.SYSTIME){
            FrameMgr.getInstance().addControll(this.frameItem);
        }else{
            this.shield_timer.text = "00m00m00";
        }

    }

    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    private static _instance:ShieldCommentPanel;
    public static getInstance():ShieldCommentPanel{
        if( ShieldCommentPanel._instance == null )
            ShieldCommentPanel._instance = new ShieldCommentPanel();
        return ShieldCommentPanel._instance;
    }
}